import CryptoJS from 'crypto-js'
export function deepClone(obj) {
  const _toString = Object.prototype.toString;

  // null, undefined, non-object, function
  if (!obj || typeof obj !== "object") {
    return obj;
  }

  // DOM Node
  if (obj.nodeType && "cloneNode" in obj) {
    return obj.cloneNode(true);
  }

  // Date
  if (_toString.call(obj) === "[object Date]") {
    return new Date(obj.getTime());
  }

  // RegExp
  if (_toString.call(obj) === "[object RegExp]") {
    const flags = [];
    if (obj.global) {
      flags.push("g");
    }
    if (obj.multiline) {
      flags.push("m");
    }
    if (obj.ignoreCase) {
      flags.push("i");
    }

    return new RegExp(obj.source, flags.join(""));
  }

  const result = Array.isArray(obj)
    ? []
    : obj.constructor
    ? new obj.constructor()
    : {};

  for (const key in obj) {
    result[key] = deepClone(obj[key]);
  }

  return result;
}
export function decryptData(encryptedData) {
    if (typeof encryptedData !== 'string') return null
    const SECRET_KEY = CryptoJS.enc.Utf8.parse("SJXUETU2024XGY45"); // 与Java端保持一致
    const encryptedBytes = CryptoJS.enc.Base64.parse(encryptedData);
    const decryptedBytes = CryptoJS.AES.decrypt({ciphertext: encryptedBytes}, SECRET_KEY, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    });
    const result = decryptedBytes.toString(CryptoJS.enc.Utf8);
    if (typeof result === 'string') {
        try {
            return {
                data: JSON.parse(result)
            }
        } catch (error) {
            console.error('OSS密钥解密失败')
            return null
        }
    }
    return null
}